package com.example.houshaung.springBootProject.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.houshaung.springBootProject.entity.UserEntity;
import com.example.houshaung.springBootProject.mapper.UserMapper;


@Controller
public class UserController {
	
	@Autowired
	private UserMapper userMapper;
	
	@RequestMapping("/getUsers")
	public String getUsers(Model model) {
		List<UserEntity> users=userMapper.getAll();
		model.addAttribute("name", users.get(0).getUserName());
		return "hello";
	}
	
	@RequestMapping("/show")
	public String getView() {
		return "index";
	}
	@RequestMapping("/hello")
	public String hello() {
		return "hello";
	}
    @RequestMapping("/getUser")
    public UserEntity getUser(Long id) {
    	UserEntity user=userMapper.getOne(id);
        return user;
    }
    
    @RequestMapping("/add")
    public void save(UserEntity user) {
    	userMapper.insert(user);
    }
    
    @RequestMapping(value="update")
    public void update(UserEntity user) {
    	userMapper.update(user);
    }
    
    @RequestMapping(value="/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
    	userMapper.delete(id);
    }
    
    
}
